﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ImageAnalysis
{
    public partial class frmImageBounds : Form
    {
        private Point pIni = new Point();

        private Point pWidth = new Point();

        private Point pHeight = new Point();

        private bool pIniSetted = false;

        private bool pWidthSetted = false;

        private bool pHeightSetted = false;

        public Rectangle ValidRegion { get; set; }

        public frmImageBounds()
        {
            InitializeComponent();
        }

        private void picImageBounds_MouseClick(object sender, MouseEventArgs e)
        {
            if (!pIniSetted || !pWidthSetted || !pHeightSetted)
            {
                if (!pIniSetted)
                {
                    pIni = e.Location;
                    pIniSetted = true;
                }
                else if (!pWidthSetted)
                {
                    pWidth = e.Location;
                    pWidthSetted = true;
                }
                else if (!pHeightSetted)
                {
                    pHeight = e.Location;
                    pHeightSetted = true;
                    btnBuildRectangle.Enabled = true;
                }
                Graphics draw = picImageBounds.CreateGraphics();
                draw.FillEllipse(Brushes.Black, e.X, e.Y, 10, 10);
            }
        }

        private void btnClearRegion_Click(object sender, EventArgs e)
        {
            pIniSetted = false;
            pWidthSetted = false;
            pHeightSetted = false;
            btnBuildRectangle.Enabled = false;
            picImageBounds.Refresh();
        }

        private void btnBuildRectangle_Click(object sender, EventArgs e)
        {
            picImageBounds.Refresh();
            Graphics g = picImageBounds.CreateGraphics();
            Pen p = new Pen(Brushes.Black);
            ValidRegion = new Rectangle(pIni, new Size(Math.Abs(pIni.X - pWidth.X), Math.Abs(pIni.Y - pHeight.Y)));
            g.DrawRectangle(p, ValidRegion);
        }
    }
}
